package com.credithc.eagleeye.api.web;
import com.credithc.eagleeye.api.constants.ResponseCodeEnum;
import com.credithc.eagleeye.api.protocol.AlertLogSearchRes;
import com.credithc.eagleeye.api.service.SettingService;
import com.credithc.eagleeye.api.vo.AlertLogSearchConditionVo;
import com.credithc.eagleeye.api.vo.AlertLogVo;
import com.credithc.eagleeye.api.vo.AlertRuleVo;
import com.credithc.eagleeye.api.vo.PageVo;
import com.credithc.eagleeye.api.vo.PlatformVo;
import com.credithc.eagleeye.api.vo.ResultVo;
import com.credithc.eagleeye.api.vo.SettingVo;
import com.credithc.eagleeye.api.vo.UserVo;
import com.credithc.eagleeye.util.FastJsonConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2018年02月07日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@SuppressWarnings("Duplicates")
@Slf4j
@Controller
public class SettingsApiCtrl extends BaseApiCtrl {

    @Autowired
    private SettingService settingService;

    @RequestMapping(value = "/settings/loadSetting", method = {RequestMethod.GET})
    @ResponseBody
    public ResultVo<String> loadSetting( ModelMap map) {
        ResultVo<String> resultVo=new ResultVo();
        try {
            SettingVo settingVo = this.settingService.loadSetting();
            if (settingVo == null) {
                settingVo = new SettingVo();
                settingVo.setUserInfoTypeList(new ArrayList<String>());
            }
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
            resultVo.setData(FastJsonConvert.convertObjectToJsonWithPrettyFormat(settingVo));
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/settings/saveSetting", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo saveSetting(@RequestBody SettingVo settingVo) {
        ResultVo resultVo=new ResultVo();
        try {
            if(settingVo!=null) {
                settingService.saveSetting(settingVo);
            }
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }


    @RequestMapping(value = "/settings/searchUserList", method = {RequestMethod.GET})
    @ResponseBody
    public ResultVo<List<UserVo>> searchUserList( ModelMap map) {
        ResultVo<List<UserVo>> resultVo=new ResultVo<>();
        try {
            List<UserVo> userVoList = this.settingService.loadAllUser();
            if (userVoList == null || userVoList.size() == 0) {
                userVoList = new ArrayList<>();
                userVoList.add(new UserVo());
            }
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
            resultVo.setData(userVoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/settings/saveUser", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo saveUser(@RequestBody UserVo userVo) {
        ResultVo resultVo=new ResultVo();
        try {
            settingService.saveUser(userVo);
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }
    @RequestMapping(value = "/settings/deleteUser", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo deleteUser(@RequestBody UserVo userVo) {
        ResultVo resultVo=new ResultVo();
        try {
            settingService.deleteUser(userVo);
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }




    @RequestMapping(value = "/settings/searchPlatformList", method = {RequestMethod.GET})
    @ResponseBody
    public ResultVo<List<PlatformVo>> searchPlatformList( ModelMap map) {
        ResultVo<List<PlatformVo>> resultVo=new ResultVo<>();
        try {
            List<PlatformVo> platformVoList = this.settingService.loadAllPlatform();
            if (platformVoList == null || platformVoList.size() == 0) {
                platformVoList = new ArrayList<>();
                platformVoList.add(new PlatformVo());
            }
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
            resultVo.setData(platformVoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }

    @RequestMapping(value = "/settings/savePlatform", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo savePlatform(@RequestBody PlatformVo platformVo) {
        ResultVo resultVo=new ResultVo();
        try {
            settingService.savePlatform(platformVo);
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }
    @RequestMapping(value = "/settings/deletePlatform", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo deletePlatform(@RequestBody PlatformVo platformVo) {
        ResultVo resultVo=new ResultVo();
        try {
            settingService.deletePlatform(platformVo);
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/settings/searchAlertRuleList", method = {RequestMethod.GET})
    @ResponseBody
    public ResultVo<List<AlertRuleVo>> searchAlertRuleList() {
        ResultVo<List<AlertRuleVo>> resultVo=new ResultVo<>();
        try {
            List<AlertRuleVo> alertRuleVoList = this.settingService.loadAllAlertRule();
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
            resultVo.setData(alertRuleVoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }

    @RequestMapping(value = "/settings/saveAlertRule", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo saveAlertRule(@RequestBody AlertRuleVo alertRuleVo) {
        ResultVo resultVo=new ResultVo();
        try {
            settingService.saveAlertRule(alertRuleVo);
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }
    @RequestMapping(value = "/settings/deleteAlertRule", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo deleteAlertRule(@RequestBody AlertRuleVo alertRuleVo) {
        ResultVo resultVo=new ResultVo();
        try {
            settingService.deleteAlertRule(alertRuleVo);
            resultVo.setCode("0000");
            resultVo.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode("0");
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }


    @RequestMapping(value = "/settings/searchAlertLogList", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo<List<AlertLogVo>> searchAlertLogList(@RequestBody AlertLogSearchConditionVo conditionVo) {
        ResultVo<List<AlertLogVo>> resultVo=new ResultVo<>();
        try {
            AlertLogSearchRes alertLogSearchRes = this.settingService.searchAlertLog(conditionVo);
            resultVo.setCode(ResponseCodeEnum.SUCCESS.getValue());
            resultVo.setData(alertLogSearchRes.getAlertLogVoList());
            PageVo pageVo = new PageVo();
            pageVo.setTotalCount(alertLogSearchRes.getTotalCount());
            resultVo.setPage(pageVo);
            resultVo.setMsg("获取AlertLog成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode(ResponseCodeEnum.ERROR_SYSTEM.getValue());
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }
}
